package com.nebula.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nebula.common.Const;
import com.nebula.common.ServerResponse;
import com.nebula.pojo.FriendsSession;
import com.nebula.pojo.FriendsShare;
import com.nebula.pojo.User;
import com.nebula.service.IFileAndFolderListService;
import com.nebula.service.IFriendsService;
import com.nebula.vo.FileAndFolderListVo;
import com.nebula.vo.UserVo;

import oracle.net.aso.s;

@Controller
@RequestMapping("/friends")
public class FriendsController {
	
	@Autowired
	private IFriendsService friendsService;
	@Autowired
	private IFileAndFolderListService fileAndFolderListService;
	
	@RequestMapping("/share")
	public String friends(){
		return "friends-share";
	}
	
	@RequestMapping("/search-friend")
	@ResponseBody
	public ServerResponse<UserVo> searchFriend(HttpSession session,@RequestParam("str") String str){
		User user = (User) session.getAttribute(Const.USERINFO);
		if(str.toLowerCase().equals(user.getUsername().toLowerCase()) || str.equals(user.getEmail())){
			UserVo userVo = new UserVo();
			userVo.setUsername(user.getUsername());
			userVo.setNickname(user.getNickname());
			return ServerResponse.createBySuccess("（自己）",userVo);
		}
		return friendsService.searchFriend(str,user.getUsername());
	}
	
	@RequestMapping("/add-friend")
	@ResponseBody
	public ServerResponse addFriend(HttpSession session,@RequestParam("respondent") String respondent
			,@RequestParam("message") String message){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.addFriend(user.getUsername(), respondent, message);
	}
	
	@RequestMapping("/application")
	@ResponseBody
	public ServerResponse applicationList(HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.applicationList(user.getUsername());
	}
	
	@RequestMapping("/agree-application")
	@ResponseBody
	public ServerResponse agreeApplication(HttpSession session,@RequestParam("applicant") String applicant){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.agreeApplication(user.getUsername(), applicant);
	}
	
	@RequestMapping("/update-view")
	@ResponseBody
	public ServerResponse updateView(HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.updateView(user.getUsername());
	}
	
	@RequestMapping("/friend-list")
	@ResponseBody
	public ServerResponse friendList(HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.friendsList(user.getUsername());
	}
	
	@RequestMapping("/set-remark")
	@ResponseBody
	public ServerResponse setRemark(HttpSession session,@RequestParam("friendname") String friendname
			,@RequestParam("remark") String remark){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.setRemark(user.getUsername(), friendname, remark);
	}
	
	@RequestMapping("/delete-friend")
	@ResponseBody
	public ServerResponse deleteFriends(HttpSession session,@RequestParam("friendname") String friendname){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.deleteFriends(user.getUsername(), friendname);
	}
	
	@RequestMapping(value = "/share-file",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse shareFile(HttpSession session,@RequestParam("friendname") String friendname
			,@RequestParam("valArr") String valArr,@RequestParam("title") String title){
		User user = (User) session.getAttribute(Const.USERINFO);
		FriendsShare friendsShare = new FriendsShare();
		friendsShare.setFileIdGroup(valArr);
		friendsShare.setTitle(title);
		friendsShare.setDistributors(user.getUsername());
		friendsShare.setReceiver(friendname);
		
		return friendsService.shareFile(friendsShare);
	}
	
	@RequestMapping(value = "/pl-share-file",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse plShareFile(HttpSession session,@RequestParam("friendname") String friendname
			,@RequestParam("valArr") String valArr,@RequestParam("title") String title){
		User user = (User) session.getAttribute(Const.USERINFO);
		FriendsShare friendsShare = new FriendsShare();
		friendsShare.setFileIdGroup(valArr);
		friendsShare.setTitle(title);
		friendsShare.setDistributors(user.getUsername());
		String []str = friendname.split(",");
		int index = 0;
		for(int i = 0,len = str.length;i < len;i++){
			friendsShare.setReceiver(str[i].trim());
			ServerResponse responseServer = friendsService.shareFile(friendsShare);
			if(responseServer.isSuccess()){
				index += 1;
			}
		}
		if(index == str.length){
			return ServerResponse.createBySuccessMessage("分享成功");
		}
		return ServerResponse.createByErrorMessage("分享失败");
	}
	
	@RequestMapping(value = "/session-list",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse sessionList(HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.sessionList(user.getUsername());
	}
	
	@RequestMapping(value = "/delete-session-list",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse deleteSessionList(HttpSession session,@RequestParam("id") Integer id){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.deleteSessionList(id,user.getUsername());
	}
	
	@RequestMapping(value = "/get-share-list",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse getFriendsShareList(HttpSession session,@RequestParam("id") int id){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.getFriendsShareList(id, user);
	}
	
	@RequestMapping(value = "/del-share-list",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse plDeleteFriendsShareList(@RequestParam("valArr") String valArr){
		return friendsService.deleteFriendsShareList(valArr);
	}
	
	@RequestMapping(value = "/get-friends-count",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse getFriendsCount(HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		return friendsService.getFriendsCount(user.getUsername());
	}
}
